var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var Pagination = require('../../common/pagination/index.js');
var app = getApp();
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,

        tabs: ['普通订单', '砍价订单'],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        sliderWidth,
    },

    onLoad: function(options) {
        this._options = options;

        common.initApp({
            extSuc: () => {
                this._initTabs(options.tabIndex || 0);
            },
            loginSuc: () => {
                this._getOrderList();
                this._getCutOrderList();
                this.$loginSuc();
            },
            sysInfoSuc: (sysInfo) => {
                this.setData({
                    winHei: app.getWinHei(),
                });
            },
        });
    },

    onPullDownRefresh() {
        this._pagination && this._pagination.flush(() => {
            wx.stopPullDownRefresh();
        });
        this._pagination1 && this._pagination1.flush(() => {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom() {
        var activeIndex = this.data.activeIndex;

        if (activeIndex == 0) {
            this._pagination && this._pagination.loadNext();
        } else if (activeIndex == 1) {
            this._pagination1 && this._pagination1.loadNext();
        }
    },

    _initTabs(index = 0) {
        wx.getSystemInfo({
            success: (res) => {
                var {
                    windowWidth
                } = res;
                var tabLen = this.data.tabs.length;

                this.setData({
                    activeIndex: index,
                    sliderLeft: (windowWidth / tabLen - sliderWidth) / 2,
                    sliderOffset: windowWidth / tabLen * index
                });
            }
        });
    },

    tabClick: function(e) {
        var id = e.currentTarget.id;
        var formId = e.detail.formId;

        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: id
        });
        common.submitForm(formId);
    },

    /**
     * 获取订单列表
     */
    _getOrderList: function() {
        this._pagination = new Pagination({
            page: this,
            scope: 'pagination'
        }).init({
            rendData: (page, cb) => {

                ajax.G({
                    url: '/market/Home/completedOrderList',
                    data: {
                        page: page
                    },
                    success: (res) => {
                        var {
                            order_list: orderList = []
                        } = res.result;
                        var allList = this.data.orderList || [];

                        this.setData({
                            dataLoaded: true,
                            orderList: page == 1 ? orderList : allList.concat(orderList),
                        });

                        cb && cb(orderList.length);

                    }
                });
            }
        });
    },

    /**
     * 获取砍价列表
     * @return {[type]} [description]
     */
    _getCutOrderList() {
        this._pagination1 = new Pagination({
            page: this,
            scope: 'pagination1'
        }).init({
            rendData: (page, cb) => {

                ajax.G({
                    url: '/market/Activity/orderList',
                    data: {
                        page: page
                    },
                    success: (res) => {
                        var {
                            orderList = []
                        } = res.result;
                        var allList = this.data.orderList || [];

                        this.setData({
                            dataLoaded: true,
                            cutOrderList: page == 1 ? orderList : allList.concat(orderList),
                        });

                        cb && cb(orderList.length);

                    }
                });
            }
        });
    },

}));